package com.test;

import com.test.entity.User;
import com.test.mapper.UserMapper;
import com.test.mapper.UserMapperAnnotation;
import com.test.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class UserMybatisAnnotation {

    private SqlSession sqlSession;

    private UserMapperAnnotation userMapperAnnotation;

    @Before
    public void setUp() {
        // 创建测试表
//        DatabaseUtil.createTestTable();

        // 获取SqlSession和Mapper
        sqlSession = MyBatisUtil.getSqlSession();
        userMapperAnnotation = sqlSession.getMapper(UserMapperAnnotation.class);

        System.out.println("🚀 开始执行MyBatis CRUD测试...");
    }

    @Test
    public void insertUser(){
        User user = new User("张三123", "111@qq.com", 18);
        int num = userMapperAnnotation.insertUser(user);
        System.out.println("插入成功，条数是：" + num);
    }

    @Test
    public void queryList(){
        List<User> userList = userMapperAnnotation.queryUserList();
        userList.forEach(bean ->{
            System.out.println(bean.getName());
        });
    }

    @Test
    public void updateUserById(){
        User user = new User(7, "张三12", "", 18);
        userMapperAnnotation.updateUserById(user);
        System.out.println("修改成功。");
    }

    @Test
    public void deleteUserById(){
        userMapperAnnotation.deleteUserById(null);
        System.out.println("删除成功。");
    }

}
